//
//  HYQViewController.m
//  HYQPickerView
//
//  Created by zxj1989 on 08/28/2018.
//  Copyright (c) 2018 zxj1989. All rights reserved.
//

#import "HYQViewController.h"
#import "HYQPickerView.h"
#import "HYQDatePickerView.h"
#import "HYQDatePickerHelper.h"

@interface HYQViewController ()

@property (nonatomic, strong) UILabel *normalLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UIButton *normalPickerButton;
@property (nonatomic, strong) UIButton *datePickerButton;
@property (nonatomic, strong) UIDatePicker *myDatePicker;

@property (nonatomic, strong) NSString *selectedHeight;
@property (nonatomic, strong) NSDate *selectedDate;

@end

@implementation HYQViewController

- (UILabel *)normalLabel
{
    if (!_normalLabel) {
        _normalLabel = [[UILabel alloc] init];
        _normalLabel.font = [UIFont systemFontOfSize:20];
        _normalLabel.textColor = [UIColor whiteColor];
        _normalLabel.backgroundColor = [UIColor redColor];
    }
    return _normalLabel;
}

- (UILabel *)dateLabel
{
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc] init];
        _dateLabel.font = [UIFont systemFontOfSize:20];
        _dateLabel.textColor = [UIColor whiteColor];
        _dateLabel.backgroundColor = [UIColor redColor];
    }
    return _dateLabel;
}

- (UIButton *)normalPickerButton
{
    if (!_normalPickerButton) {
        _normalPickerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _normalPickerButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_normalPickerButton setTitle:@"选择身高" forState:UIControlStateNormal];
        [_normalPickerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_normalPickerButton addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        _normalPickerButton.backgroundColor = [UIColor redColor];
    }
    return _normalPickerButton;
}

- (UIButton *)datePickerButton
{
    if (!_datePickerButton) {
        _datePickerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _datePickerButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_datePickerButton setTitle:@"选择时间" forState:UIControlStateNormal];
        [_datePickerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_datePickerButton addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        _datePickerButton.backgroundColor = [UIColor redColor];
    }
    return _datePickerButton;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    
    [self.view addSubview:self.normalLabel];
    [self.view addSubview:self.dateLabel];
    [self.view addSubview:self.normalPickerButton];
    [self.view addSubview:self.datePickerButton];
    self.normalLabel.text = @"已选择身高：";
    self.dateLabel.text = @"已选择时间：";
    self.normalLabel.frame = CGRectMake(20, 30, CGRectGetWidth(self.view.bounds) - 40, 30);
    self.dateLabel.frame = CGRectMake(20, 80, CGRectGetWidth(self.view.bounds) - 40, 30);
    
    self.normalPickerButton.frame = CGRectMake(20, 150, 100, 40);
    self.datePickerButton.frame = CGRectMake(200, 150, 100, 40);
    
}

- (void)btnAction:(UIButton *)btn
{
    if (btn == self.normalPickerButton) {
        [self testPicker];
    }else if (btn == self.datePickerButton) {
        [self testDatePicker];
    }
}

- (void)testPicker
{
    HYQPickerView *pickerView = [[HYQPickerView alloc] initWithFrame:self.view.bounds];
    NSArray *arr = @[@[@"170cm", @"171cm", @"172cm", @"173cm", @"174cm", @"175cm", @"176cm", @"177cm", @"178cm"]];
    pickerView.componentDataArray = arr;
    NSInteger selectedIndex = -1;
    if (self.selectedHeight) {
        for (int i = 0; i < [arr[0] count]; i++) {
            if ([self.selectedHeight isEqualToString:arr[0][i]]) {
                selectedIndex = i;
                break;
            }
        }
    }
    [pickerView setRowSelected:selectedIndex component:0];
    pickerView.completedBlock = ^(NSArray *indexPathArray) {
        NSString *result = nil;
        for (int i = 0; i < indexPathArray.count; i++) {
            NSIndexPath *indexPath = indexPathArray[i];
            if (indexPath.section == 0) {
                self.selectedHeight = arr[indexPath.section][indexPath.row];
                result = [NSString stringWithFormat:@"已选择身高：%@",self.selectedHeight];
            }
        }
        self.normalLabel.text = result;
    };
    [pickerView showInView:self.view];
}

- (void)testDatePicker
{
    HYQDatePickerView *datePicker = [[HYQDatePickerView alloc] initWithFrame:self.view.bounds];
    datePicker.mode = HYQDatePickerViewModeDate;
    datePicker.selectedDate = self.selectedDate;
//    datePicker.maxDate = [NSDate date];
    datePicker.minDate = [NSDate date];
    datePicker.datePickerViewDidSelectedDateBlock = ^(NSDate *date) {
        self.selectedDate = date;
        self.dateLabel.text = [NSString stringWithFormat:@"已选择时间：%@", [HYQDatePickerHelper stringFromDate:date]];
    };
    [datePicker showInView:self.view];
}

- (void)addDatePicker
{
    _myDatePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 50, [UIScreen mainScreen].bounds.size.width, 300)];
    
    // 设置日期选择控件的地区
    
    [_myDatePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];
    
    //    [myDatePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_SC"]];
    
    //默认为当天。
    
    [_myDatePicker setCalendar:[NSCalendar currentCalendar]];
    
    //    设置DatePicker的时区。
    
    //    默认为设置为：[datePicker setTimeZone:[NSTimeZone defaultTimeZone]];
    
    //    设置DatePicker的日期。
    
    //    默认设置为:
    
    [_myDatePicker setDate:[NSDate date]];
    
    //    minimumDate设置DatePicker的允许的最小日期。
    
    //    maximumDate设置DatePicker的允许的最大日期
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDate *currentDate = [NSDate date];
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    
    [comps setDay:10];//设置最大时间为：当前时间推后10天
    
    NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
    
    [comps setDay:0];//设置最小时间为：当前时间
    
    NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
    
//    [_myDatePicker setMaximumDate:maxDate];
//
//    [_myDatePicker setMinimumDate:minDate];
    
    
    
    
    
    
    
    //    设置DatePicker的倒计时间.
    
    //    1) 设置日期选择的模
    
        [_myDatePicker setDatePickerMode:UIDatePickerModeDateAndTime];
    
    //    2) 设置倒计时的时长
    
    //    注意：设置倒计时时长需要在确定模式之后指定
    
    // 倒计时的时长，以秒为单位
    
    //    [myDatePicker setCountDownDuration:10 * 60];
    
    
    
    
    
    //显示小时，分钟和AM/PM
    
    //    [myDatePicker setDatePickerMode:UIDatePickerModeTime];
    
    //显示年月日
    
    //    [myDatePicker setDatePickerMode:UIDatePickerModeDate];
    
    //显示小时和分钟
    
    //    [myDatePicker setDatePickerMode:UIDatePickerModeCountDownTimer];
    
    
    
    //监听datepicker值的改变
    
    [_myDatePicker addTarget:self action:@selector(dateChange:)forControlEvents:UIControlEventValueChanged];
    
    
    
    [self.view addSubview:_myDatePicker];
}

//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    HYQPickerView *pker = [[HYQPickerView alloc] initWithFrame:self.view.bounds];
//    pker.componentDataArray = @[@[@"11111",@"2222222",@"3333"], @[@"11111",@"2222222",@"3333"], @[@"11111",@"2222222",@"3333"]];
//    [pker showInView:self.view];
//    [pker setRowSelected:0 component:0];
//    pker.completedBlock = ^(NSArray *indexPathArray) {
//
//    };
//}

- (void)dateChange:(UIControl *)control
{
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
